package com.service.lotterySystem.service.impl;

import com.service.config.utils.IdUtil;
import com.service.config.utils.ModelMapperUtil;
import com.service.config.utils.Result;
import com.service.lotterySystem.dao.UserDataInfoDao;
import com.service.lotterySystem.dao.UserInfoDao;
import com.service.lotterySystem.dto.UserDataDto;
import com.service.lotterySystem.dto.UserInfoAndDataDto;
import com.service.lotterySystem.entity.UserDataInfo;
import com.service.lotterySystem.entity.UserInfo;
import com.service.lotterySystem.service.UserDataService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author ：hjl
 * @date ：Created in 2022/01/11 17:38
 * @description：
 */
@Service
@Slf4j
public class UserDataServiceImpl implements UserDataService {
    @Resource
    private UserDataInfoDao userDataDao;
    @Resource
    private UserInfoDao userInfoDao;

    @Override
    public UserInfoAndDataDto queryUserById(String uid) {
        UserInfo userInfo = userInfoDao.queryById(uid);
        UserInfoAndDataDto userInfoAndDataDto = ModelMapperUtil.strictMap(userInfo, UserInfoAndDataDto.class);
        UserDataInfo userExtendInfo=userDataDao.queryUserDataByUid(uid);
        if (userExtendInfo!=null)
        {
            if (userExtendInfo.getCity() != null && !userExtendInfo.getCity().equals(""))  {
                userInfoAndDataDto.setCity(userExtendInfo.getCity());
            }
            if (userExtendInfo.getCompanyName() != null && !userExtendInfo.getCompanyName().equals(""))  {
            userInfoAndDataDto.setCompanyName(userExtendInfo.getCompanyName());
        }
            if (userExtendInfo.getName() != null && !userExtendInfo.getName().equals(""))  {
            userInfoAndDataDto.setName(userExtendInfo.getName());
        }
            if (userExtendInfo.getPhone() != null && !userExtendInfo.getPhone().equals(""))  {
            userInfoAndDataDto.setPhone(userExtendInfo.getPhone());
        }
            return userInfoAndDataDto;
        }
        return userInfoAndDataDto;
    }

    /**
     * 增加用户资料
     * @param userDataDto
     * @return
     */
    @Override
    public Result addUserData(UserDataDto userDataDto) {
        UserDataInfo userDataInfo = ModelMapperUtil.strictMap(userDataDto, UserDataInfo.class);
        userDataInfo.setId(IdUtil.getStringId());
        userDataDao.insert(userDataInfo);
        return Result.ok();
    }

    /**
     * 修改用户资料
     * @param userDataDto
     * @return
     */
    @Override
    public Result updateUserData(UserDataDto userDataDto) {
        UserDataInfo userDataInfo1 = userDataDao.queryUserDataByUid(userDataDto.getuId());
        if (userDataInfo1 != null) {
            UserDataInfo userDataInfo = ModelMapperUtil.strictMap(userDataDto, UserDataInfo.class);
            userDataDao.update(userDataInfo);
        }else {
            UserDataInfo userDataInfo = ModelMapperUtil.strictMap(userDataDto, UserDataInfo.class);
            userDataInfo.setId(IdUtil.getStringId());
            userDataDao.insert(userDataInfo);
        }
        UserInfo userInfo = new UserInfo();
        userInfo.setId(userDataDto.getuId());
        userInfo.setNickName(userDataDto.getNickName());
        userInfoDao.update(userInfo);
        return Result.ok();
    }
}
